#include "recovery/fs/file/FileRandomWriter.h"

#include "recovery/fs/file/File.h"
namespace recovery {
namespace fs {
namespace file {

ErrnoStatus FileRandomWriter::openExist(const std::string & path, std::unique_ptr<FileRandomWriter> & writer){
    writer.reset(new FileRandomWriter(path));
    return writer->file().openForRandomWrite(writer->writableFd());
}
ErrnoStatus FileRandomWriter::create(const std::string & path, std::unique_ptr<FileRandomWriter> & writer){
    writer.reset(new FileRandomWriter(path));
    return writer->file().createForRandomWrite(writer->writableFd());
}
FileRandomWriter::FileRandomWriter(const std::string & path)
: AbstractFileOperation(path, true)
{
}

ErrnoStatus FileRandomWriter::write(const char * buf, const size_t len, size_t & writedBytes){
    return file().write(fd(), reinterpret_cast<const uint8_t *>(buf), len ,writedBytes);
}
ErrnoStatus FileRandomWriter::write(const std::string & data, size_t & writedBytes){
    return file().write(fd(), reinterpret_cast<const uint8_t *>(data.c_str()), data.size(), writedBytes);
}
ErrnoStatus FileRandomWriter::sync(){
    return file().sync(fd());
}
ErrnoStatus FileRandomWriter::closeOnce(){
    return file().close(fd());
}

} //end of namespace file
} //end of namespace fs
} //end of namespace recovery

